# Contributor: Tom Lebreux <me@tomlebreux.com>
# Maintainer: Tom Lebreux <me@tomlebreux.com>
pkgname=navidrome
pkgver=0.47.5
pkgrel=3
pkgdesc="Modern Music Server and Streamer compatible with Subsonic/Airsonic"
url="https://navidrome.org/"
arch="all !riscv64" # blocked by nodejs
license="GPL-3.0-or-later"
# check: TypeError: Cannot redefine property: localStorage
# For downloading go and nodejs dependencies
options="net !check"
depends="ffmpeg"
makedepends="go npm taglib-dev zlib-dev"
install="$pkgname.pre-install"
subpackages="$pkgname-openrc"
pkgusers="navidrome"
pkggroups="navidrome"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/navidrome/navidrome/archive/refs/tags/v$pkgver.tar.gz
	navidrome.initd
	navidrome.confd
	navidrome.toml
	"
# temp fix remove later
options="$options chmod-clean"

export GOFLAGS="$GOFLAGS -modcacherw -trimpath"
export GOPATH="$srcdir"

# secfixes:
#   0.47.5-r0:
#     - CVE-2022-23857

build() {
	make download-deps
	# Prevents crash due to allocation failure
	echo 'node-options=--max_old_space_size=3072' > ./ui/.npmrc
	cd ./ui/
	npm install
	npm run build
	cd ..

	# Display pkgrel instead of keeping the git sha in sync with pkgver
	CGO_ENABLED=1 go build \
		-ldflags="
			-X github.com/navidrome/navidrome/consts.gitSha=$pkgver-r$pkgrel
			-X github.com/navidrome/navidrome/consts.gitTag=v$pkgver" \
		-tags=embed,netgo
}

check() {
	make testall
}

package() {
	install -Dm755 navidrome "$pkgdir"/usr/bin/navidrome

	install -Dm644 "$srcdir"/navidrome.toml \
		"$pkgdir"/etc/navidrome/navidrome.toml

	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/$pkgname

	install -dm755 -o navidrome -g navidrome \
		"$pkgdir"/var/lib/navidrome \
		"$pkgdir"/var/lib/navidrome/music
}

sha512sums="
c3bd67b52c55d722b4d6f581c24b7cb566598db3911d5c716d2876099ec5c9c82947571d509c9df14b2c327ec9fdfd9204412848f511ca99cbf89367454c1db0  navidrome-0.47.5.tar.gz
d632f769ed4dd0cc1ddf937ed6b3115df538399f176cc5afb576f3129c305e7a0b2762b5d62968e2819ee4cd9c88552e5c821601f5fd5b32ce5925f3e92ce281  navidrome.initd
e69ff79d0be237979c7faa28cc7d6467070c48fb8870a228d122622b8fb4d4cd3cdd58bd22090bec37478b8c4cec8d30db5928c2094ee60a32c40bc7c96d10a3  navidrome.confd
c3f25ce03e9a433f845485d744a5634c2cf462bf7e52bd8f58d965d5c5bd53630cc71702b444fb0201d66a20556f8570a24a69683be93e0efdd719d2a5e0f02f  navidrome.toml
"
